<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<div class="easyui-layout" fit="true">
  <div region="center" style="padding:0px;border:0px;" split="true">
  <t:datagrid name="productList" title="" pagination="false"  onClick="mallEtcCardruleAddModule.rowclick"
              actionUrl="mallShopCardruleController.do?productdatagrid&type=2&shopid=${shopid}" idField="id" fit="true">
   <t:dgCol title="商品id" field="id"    width="120"></t:dgCol>
   <t:dgCol title="商品名称" field="productname"   width="120"></t:dgCol>
   <t:dgCol title="商品编码" field="productcode"   width="120"></t:dgCol>
   <t:dgCol title="创建时间" field="createtime" formatter="yyyy-MM-dd"  width="120"></t:dgCol>
  </t:datagrid>
  </div>
 </div>
<div region="south"  style="height: 300px" split="true">
    <form id="mallShopCardrule" action="mallEtcCardRuleController.do?save">
        <input type="hidden" value="${shopid}" name="shopid">
        <input type="hidden" id="product_id" name="productid">
        <input type="hidden" id="prefix" name="cardNoPrefix">
        <table style="width: 600px;" cellpadding="0" cellspacing="1" class="formtable">
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        店铺:
                    </label>
                </td>
                <td class="value">
                   <span>${shopname}</span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        商品:
                    </label>
                </td>
                <td class="value">
                   <span id="product_name" ></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        供应商:
                    </label>
                </td>
                <td class="value">
                   <select name="etcsupplierid" datatype="*">
                       <c:forEach items="${mallEtcSuppliers}" var="etcSupplier">
                           <option value="${etcSupplier.id}">
                                   ${etcSupplier.suppliername}
                           </option>
                       </c:forEach>
                   </select>
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label" >
                        开始卡号:
                    </label>
                </td>
                <td class="value">
                    <input class="" id="start_cardno"  datatype="d,cardrule">
                    <input type="hidden" name="startCardNo" id="start_cardno_val">
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        结束卡号:
                    </label>
                </td>
                <td class="value">
                    <input class="" id="end_cardno"  datatype="d,cardrule">
                    <input type="hidden" name="endCardNo" id="end_cardno_val">
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        待生成ETC卡张数:
                    </label>
                </td>
                <td class="value">
                    <span id="card_num">0</span>
                </td>
            </tr>
            <tr>
                <td align="right">
                </td>
                <td class="value">
                    <input type="button" value="确定" id="save">
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
        </table>
    </form>
</div>

<script type="text/javascript">
    $(function() {
       // mallEtcCardruleAddModule.init();
    });
    var mallEtcCardruleAddModule = (function(){
        //表单验证
        var fromValidform = function(){
            $("#mallShopCardrule").Validform({
                tiptype:4,
                btnSubmit:"#save",
                ajaxPost:true,
                datatype:{
                    //ETC卡验证
                    "cardrule":function(gets,obj,curform,regxp){
                        var isValid = true;
                        var msg;
                        //比较结束卡号和开始卡号
                        var startV = $("#start_cardno").val();
                        var endV = gets;
                        //获取开始和结算的长度
                        var startLength = startV.length;
                        var endLength = endV.length;
                        //比较开始长度和结算长度
                        if(startLength != endLength){//不相等
                            msg = "开始卡号和结束卡号长度不相等";
                            isValid = false;
                        }else{//长度相等
                            if(startV == endV){//开始等于结束
                                //获取前缀
                                var lengthEqualSprefix = startV.substring(0, startLength - 1);
                                //后缀
                                var suffix = startV.substr(startLength - 1, startLength);
                                //设置前缀，后缀开始，后缀结束
                                $("#prefix").val(lengthEqualSprefix);
                                $("#start_cardno_val").val(suffix);
                                $("#end_cardno_val").val(suffix);
                                var num = 1;
                                $("#card_num").text(num);
                            }else{//开始结束不相等
                                //找出相等部分
                                var startArray = startV.split("");
                                var endArray = endV.split("");
                                var m = 0;
                                for(var i = 0; i < startArray.length; i++){
                                    if(startArray[i] != endArray[i]){
                                        m = i;
                                        break;
                                    }
                                }
                                //获取前缀
                                var prefix = startV.substring(0, m);
                                //获取开始卡号后缀
                                var startNoSuffix = startV.substr(m, startV.length);
                                //获取结束卡号后缀
                                var endNoSuffix = endV.substr(m, startV.length);
                                if(endNoSuffix.length > 3){//不同部分长度大于3就不计算
                                    msg = "卡号张数不能超过1000张";
                                    isValid = false;
                                }else{
                                    if(parseInt(endNoSuffix) <= parseInt(startNoSuffix)){
                                        msg = "结束卡号不能小于开始卡号";
                                        isValid = false;
                                    }else{
                                        //设置前缀，后缀开始，后缀结束
                                        $("#prefix").val(prefix);
                                        $("#start_cardno_val").val(startNoSuffix);
                                        $("#end_cardno_val").val(endNoSuffix);
                                        var num = parseInt(endNoSuffix) - parseInt(startNoSuffix) + 1;
                                        $("#card_num").text(num);
                                    }
                                }
                            }
                        }
                        if(isValid){
                            return true;
                        }else{
                            return msg;
                        }
                    }
                },
                beforeSubmit:function(){
                    console.log($("#mallShopCardrule").serialize());
                    return true;
                },
                callback:function(data){
                    if(data.success==true){
                        $.messager.alert('成功',data.msg,"",function(){
                            window.document.location.href  = 'mallEtcCardRuleController.do?list'
                        });
                    }else{
                        $.messager.alert('错误',data.msg);
                    }
                }
            });
        }


        var module = {
            init :function(){
                fromValidform();
            },
           rowclick:function(rowIndex, rowData){
                var productId = rowData.id;
                var productName = rowData.productname;
                $("#product_id").val(productId);
                $("#product_name").text(productName);
               fromValidform();
            }
        }
        return module;
    })();
</script>
